﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace plagiat_tp
{
    class TextFunctions
    {
        string[] SourceControlSumm;

        public void SourceText(RichTextBox richTextBox1, Label address)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            Stream myStream = null;


            openFileDialog1.Filter = "RTF files|*.rtf|Text files|*.txt|All files|*.*";
            openFileDialog1.FilterIndex = 3;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            Stream s = openFileDialog1.OpenFile();
                            try
                            {
                                richTextBox1.LoadFile(openFileDialog1.FileName,
                                RichTextBoxStreamType.RichText);
                                s.Close();
                            }
                            catch (System.ArgumentException)
                            {
                                richTextBox1.LoadFile(openFileDialog1.FileName,
                                RichTextBoxStreamType.PlainText);
                                s.Close();
                            }
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
                //address.Text=openFileDialog1.FileName;
                String[] strArr = openFileDialog1.FileName.Split('\\');
                address.Text =  strArr[0] + "\\..\\" + strArr[strArr.Length - 1];
               

            }

        }
        public string[] getSourceTextParam(RichTextBox richTextBox1, Label address)
        {
            SourceText(richTextBox1, address);
            Canonize viv = new Canonize();
            string str = viv.CanonText(richTextBox1.Text);
            Shingle sh = new Shingle();
            SourceControlSumm= sh.GetControlSumms(str);
            return SourceControlSumm;
        }

        public void getArrOfFiles(ListBox listBox1)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "RTF files|*.rtf|Text files|*.txt";
            openFileDialog1.FilterIndex = 3;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;

            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    foreach (string str in openFileDialog1.FileNames)
                        listBox1.Items.Add(str);
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error:  " + ex.Message);
            }
        }

        public string GetFileContent(string url)
        {
            StreamReader sr = new StreamReader(url,Encoding.GetEncoding(1251));         
            return sr.ReadToEnd();
        }

      
        /// <summary>
        /// Получает текст в диалоговом окне при двойном клике
        /// </summary>
        /// <param name="richTextBox2"></param>
        /// <param name="listBox1"></param>
        public void GetTextFromDoubleClick(RichTextBox richTextBox2,ListBox  listBox1)
        {
            richTextBox2.Clear();
            try
            {
                richTextBox2.LoadFile(listBox1.SelectedItem.ToString(), RichTextBoxStreamType.RichText);
            }
            catch (System.ArgumentException)
            {
                richTextBox2.LoadFile(listBox1.SelectedItem.ToString(), RichTextBoxStreamType.PlainText);
            }
        }
    }

   
}
